#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include<QFontDialog>
#include<QFileDialog>
#include<QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    time = new QTimer(this);
    connect(time,&QTimer::timeout,[=]{
         static int smc=0;
        ui->lcdNumber->display(++smc);
    });

   connect(ui->pBn_start,&QPushButton::clicked,this,&MainWindow::Delay);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionNew_triggered()
{
    qDebug()<<"新建文件"<<endl;
}

void MainWindow::on_actionFront_triggered()
{
        bool ok;
        //QFontDialog::getFont() 函数弹出一个字体选择对话框，允许用户选择字体
        QFont font = QFontDialog::getFont(&ok, this);
        if(ok)
        {
           qDebug()<<"选择字体:"<<ok<<endl;
        }
        else {
           qDebug()<<"取消选择"<<endl;
        }


}



void MainWindow::on_actionOpen_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this, "Open File");
    qDebug() << "Selected File: " << fileName;

    if (!fileName.isNull()) {
        QFile file(fileName);
        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in (&file);            //对象读取文件中的文本内容
            QString fileContent = in.readAll();//将读取的文本内容存储到fileContent里面
            file.close();

            ui->textEdit->setText(fileContent);//将 fileContent 的内容设置为 ui->textEdit 的文本内容
        } else {
            qDebug() << "Failed to open file.";
        }
    }
}

void MainWindow::Delay()
{
    qDebug()<<"open"<<endl;
    static int sta=0;
    if(sta==0)
    {
        sta=1;
        time->start(1);//ms
    }
    else {
        {
            time->stop();
            sta=0;
        }
    }
   // for(int i=0;i<2000;++i)
   // time->stop();
}

void MainWindow::on_pBn_start_clicked(bool checked)
{
    if(!checked)
    {
        time->stop();
    }
}
